require 'middleclass'
require 'Player'
require 'SpriteHelper'
require 'StatusBar'

local storyboard = require "storyboard"
local physics = require("physics")
local levelParser = require("levelParser")

local cameraC = require("camera")
player = nil
local game = nil
local parentGame = nil
local backImage = nil

local scene = storyboard.newScene()

function scene:createScene( event )
    parentGame = self.view    
    
    physics.start()
    physics.setGravity(0, 0)
    --physics.setTimeStep(0)
    --physics.setPositionIterations( 2 )
    --physics.setVelocityIterations( 1 )
    
    backImage = display.newImageRect("images/background.png",2400, 1800)
    backImage.x = 1000
    backImage.y = 600
    parentGame:insert(backImage)
    
     --sheets   
    local imageSheet = graphics.newImageSheet( SpriteHelper.pathLevel, SpriteHelper.optionsLevel )
     --player sheets   
    local imageSheetPlayer = graphics.newImageSheet( SpriteHelper.pathPlayer, SpriteHelper.optionsPlayer )
    
    local planetGroup,backgroundGroup, bugGroup = levelParser.newLevel("levels/level"..event.params.level..".xml",imageSheet)
    game = display.newGroup()
    game:insert(backgroundGroup)
    game:insert(planetGroup)
    game:insert(bugGroup)
    player = Player:new(imageSheetPlayer, 5, 175,155)
    game:insert(player.body)
    game:insert(player.sprite)
    parentGame:insert(game)
    
    -- counters
    local statusBar = StatusBar:initialize(50, 10, imageSheet)
end

scene:addEventListener( "createScene", scene )

local function updateGame()
    player:update(game)
    cameraC.moveCamera(game,backImage,player)
    if(player.mode == Player.DEAD)then
        Runtime:removeEventListener("enterFrame", updateGame)
    end
end

function scene:enterScene( event )
    parentGame = self.view    
    
    cameraC.newCamera(game,player)
    
    
    Runtime:addEventListener("enterFrame", updateGame)
    
    local backButton = display.newRect( display.contentWidth - 60, 10, 50, 50 )
    backButton.alpha = 0.2
    local function doTouch(self, event)
        
        if event.phase == "began" then
            storyboard.gotoScene("LevelSelectionScene")
            return true
        end
    end
    
    backButton.touch = doTouch
    backButton:addEventListener("touch", backButton)
end

scene:addEventListener( "enterScene", scene )

function scene:destroyScene( event )   
    print('destroyScene')
    player:removeSelf()
end
scene:addEventListener( "destroyScene", scene )

function scene:exitScene( event )
    print('exitScene')
    Runtime:removeEventListener("enterFrame", updateGame)
    Runtime:dispatchEvent( {name = 'clean'} )
    physics.stop()
    storyboard.purgeScene( "LevelScene" )
end
scene:addEventListener( "exitScene", scene )

function scene:levelComplete( event )
    print('levelComplete')
    Runtime:removeEventListener("levelComplete", scene)
    Runtime:removeEventListener("enterFrame", updateGame)
    timer.performWithDelay(888, function() storyboard.gotoScene("LevelSelectionScene") end)
end
Runtime:addEventListener("levelComplete", scene)
    
return scene

